package controller;
import com.fasterxml.jackson.databind.ObjectMapper;
import model.Blog;
import model.User;
import model.UserMapper;
import model.blogMapper;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.sql.SQLException;

@WebServlet("/userInfor")
public class UserInforServlet extends HttpServlet {
    //这个doGet要同时处理博客列表页面(显示当前登录用户信息)和当点击与登录用户不一致的userId
    //博客文章时(显示与当前文章的作者信息),返回对应的User对象
    private ObjectMapper objectMapper=new ObjectMapper();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //处理是否为详情文章页面发来的请求(如果是的话blogId!=null)

        HttpSession session= req.getSession(false);
        if(session==null){
            resp.setContentType("text/html;charset=utf8");
            resp.getWriter().write("用户未登录");
            return;
        }//获取当前登陆的用户
        User user=(User)session.getAttribute("user");
        user.setAuthorSelf(0);
        //
        if(user==null){
            resp.setContentType("text/html;charset=utf8");
            resp.getWriter().write("用户未登录");
            return;
        }
        String blogId=req.getParameter("blogId");
        if(blogId==null){
            //请求来自博客列表页
           resp.setContentType("text/html;charset=utf8");
           //resp.getWriter().write("当前请求来自blog_List");

            //将获取到的User对象通过Jacson里的类转换为一个Jason字符串对象
            resp.setContentType("application/json; charset=utf8");
            String jsonString = objectMapper.writeValueAsString(user);
            //System.out.println(user+"[[[");日志打印
            resp.getWriter().write(jsonString);

        }else{
            //请求来自详情博客页
            try {
                blogMapper blogMapper=new blogMapper();
                Blog blog=blogMapper.selectOne(Integer.parseInt(blogId));
                if(blog==null){
                    resp.setContentType("text/html;charset=utf8");
                    resp.getWriter().write("当前 blogId 有误!");
                    return;
                }
                UserMapper userMapper=new UserMapper();
                int userId=blog.getUserId();
                User blogIdUser=userMapper.selectByUserId(userId);
                if(blogIdUser==null){
                    resp.setContentType("text/html;charset=utf8");
                    resp.getWriter().write("当前对应博客作者未找到");
                    return;
                }
                if(user.getUserId()==userId){
                    System.out.println("888");
                    blogIdUser.setAuthorSelf(1);
                }
                resp.setContentType("application/json; charset=utf8");
                String jsonString = objectMapper.writeValueAsString(blogIdUser);
                //System.out.println(blogIdUser+"]]]");//日志打印
                resp.getWriter().write(jsonString);

            } catch (SQLException e) {
                e.printStackTrace();
            }
        }


    }
}
